<?php

class IndexController extends Zend_Controller_Action
{
    /**
     * @return void
     */
    public function init()
    {
        /** @var Zend_Controller_Action_Helper_AjaxContext $ajaxContext */
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('send-comment', 'json')
            ->initContext();
    }

    /**
     * @return void
     */
    public function indexAction()
    {
        $productTypePOJOs = Application_Model_ProductType::getProductTypes();
        $this->view->productTypePOJOs = $productTypePOJOs;

        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $messages = $flashMessenger->getMessages();
        $this->view->messages = $messages;

        // BEGIN: Tạo đối tượng Paginator
        $dbSelect = JDbAdapter::factory()->select()
            ->from('PRODUCTS')
            ->where('Deleted = ?', false);
        $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbSelect($dbSelect)
        );

        // Tìm kiếm theo tên sản phẩm (nếu có)
        $keyWords = $this->_request->getParam('keyWords');
        if (isset($keyWords)) {
            $dbSelect->where("Name LIKE '%$keyWords%'");
            $this->view->enteredKeyWords = $keyWords;
        }

        // Lọc theo loại sản phẩm (nếu có)
        $productTypeID = $this->_request->getParam('productTypeID');
        if (isset($productTypeID)) {
            $dbSelect->where('ProductTypeID = ?', $productTypeID);
            $this->view->selectedProductTypeID = $productTypeID;
        }
        // END: Tạo đối tượng Paginator

        // Get and set current page
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        $products = $paginator->getItemsByPage($page);
        $numberOfProducts = count($products);
        for ($i = 0; $i < $numberOfProducts; $i++) {
            $products[$i]['ImageURL'] = JImageDB::getImageURL($products[$i]['Image']);
        }

        // Assign data for view
        $this->view->paginator = $paginator;
        $this->view->products = $products;
    }

    /**
     * @return void
     */
    public function detailAction()
    {
        // Get product info
        $productID = (int)$this->_request->getParam('productID');
        $productPOJO = Application_Model_Product::getProduct($productID);
        if ($productPOJO == null) {
            /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
            $flashMessenger = $this->_helper->getHelper('FlashMessenger');
            $flashMessenger->addMessage('This product is deleted');
            $this->redirect('/index/index');
        }


        $productPOJO->ImageURL = JImageDB::getImageURL($productPOJO->Image);
        $productPOJO->Expired = $productPOJO->Expired->get('d-M-y');
        $this->view->productPOJO = $productPOJO;

        // Get comments
        $commentPOJOs = Application_Model_Comment::getComments($productID);
        foreach ($commentPOJOs as $commentPOJO) {
            $commentPOJO->CreatedDate = $commentPOJO->CreatedDate->get('d-M-y');
        }
        $this->view->commentPOJOs = $commentPOJOs;
    }

    /**
     * @return void
     */
    public function sendCommentAction()
    {
        $content = $this->_request->getParam('content');
        $productID = $this->_request->getParam('productID');
        $user = JFactory::getUser();
        if (!$user->IsGuest) {
            if ($user->addComment($content, $productID) == 0) {
                $this->view->message = 'ADD COMMENT FAIL';
            } else {
                $this->view->message = 'ADD COMMENT SUCCESSFULLY';
            }
        } else {
            $this->view->message = 'YOU MUST LOGIN';
        }
    }
}
